//引入模块
import Api from './api.js'
import Cookie from './cookie.js'

new Vue({
        el:'#app',
        data(){
            return{

                zh:'',
                nc:'',
                tx:'',
                sex:'1',
                zy:'',

                //当前账户信息对象
                info:'',

                //数组地址
                city:'',
                options: [{
                            value: '荆门',
                            label: '荆门',
                            children: [{
                                value: '东宝区',
                                label: '东宝区',
                                children: [{
                                            value: '象山大道',
                                            label: '象山大道'
                                        }, {
                                            value: '长宁大道',
                                            label: '长宁大道'
                                        }, {
                                            value: '海慧路',
                                            label: '海慧路'
                                        }]
                                    }, {
                                        value: '掇刀区',
                                        label: '掇刀区',
                                        children: [{
                                            value: '白云大道',
                                            label: '白云大道'
                                        }, {
                                            value: '关公大道',
                                            label: '关公大道'
                                        }]
                                    }]
                                },
                        {
                            value: '北京',
                            label: '北京',
                            children: [{
                                value: '海淀区',
                                label: '海淀区',
                                children: [{
                                    value: '象山大道',
                                    label: '象山大道'
                                },  {
                                    value: '海慧路',
                                    label: '海慧路'
                                }]
                            }, {
                                value: '朝阳区',
                                label: '朝阳区',
                                children: [{
                                    value: '白云大道',
                                    label: '白云大道'
                                }, {
                                    value: '关公大道',
                                    label: '关公大道'
                                }]
                            }]
                        }
               ],

                //字符地址
                scity:'',


                dialogVisible: false,
                //图片地址
                imageUrl: '',
                imgurl:'',
                //加载
                fullscreenLoading: false,
                //关注的账号
                carezh:[],
                //账号的粉丝
                fanszh:[]
            }
        },
        mounted(){
            this.userinfo()
        },
        methods:{
            async userinfo(){
                //获取所有用户数据
                let info = await Api.login()
                let allinfo = info
                //获取当前用户
                let zh = sessionStorage.getItem("zh")
                //判断是否登录
                if(zh==null){
                    this.$alert('请先登录！', '温馨提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'info',
                                message: `action: ${ action }`
                            });
                            window.location.href="./login.html"
                        }
                    });
                }
                    //获取当前用户信息
                    for(let i=0;i<info.length;i++){
                        if(info[i].zh==zh){
                            info=info[i]
                            Cookie.setCookie("ninfo",JSON.stringify(info),'s7200')
                            break;
                        }
                    }
                //将用户信息绑定属性
                this.zh=info.zh
                this.nc=info.nc
                this.tx=info.tx
                if (info.sex!=null){
                    this.sex=info.sex
                }
                this.zy=info.zy
                this.info=info.info
                if(info.city!=null){
                    this.city=(info.city).split(",")
                }

                //调用关注表的内容
                let careinfo = await Api.careInfo()
                let caremsg = careinfo.data
                //获取
                for (let i=0;i<caremsg.length;i++){
                        for (let j=0;j<allinfo.length;j++){
                            //关注的账号
                            if(caremsg[i].user_zh==zh){
                                if(caremsg[i].care_zh==allinfo[j].zh){
                                    this.carezh.push(allinfo[j])
                                }
                            }
                            //粉丝的账号
                            if(caremsg[i].care_zh==zh){
                                if(caremsg[i].user_zh==allinfo[j].zh){
                                    this.fanszh.push(allinfo[j])
                                }
                            }
                        }
                }
            },

            //验证
            verify1(){
                //正则表达式
                let regExp=/^[A-Za-z0-9\u4e00-\u9fa5]{1,10}$/;
                if(!regExp.test(this.nc)){
                    this.$message({
                        showClose: true,
                        message: '您的昵称为空或输入不规范！',
                        type: 'error'
                    });
                    this.nc=JSON.parse(Cookie.getCookie("ninfo")).nc
                }
            },
            verify2(){
                let regExp=/^[\u4e00-\u9fa5]{1,10}$/;
                if(!regExp.test(this.zy)){
                    this.$message({
                        showClose: true,
                        message: '您的职业为空或输入不规范！',
                        type: 'error'
                    });
                    this.zy=JSON.parse(Cookie.getCookie("ninfo")).zy
                }
            },

            //保存信息
            save(){
                if (this.nc!=''&&this.zy!=''&&this.city!=''){
                    let info =  JSON.parse(Cookie.getCookie("ninfo"))
                    this.$confirm('是否确认更改您的信息?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        Api.save(info.id,this.nc,this.tx,this.sex,this.zy,this.scity,this.info)
                        this.$message({
                            showClose: true,
                            message: '修改成功！',
                            type: 'success'
                        });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消保存'
                        });
                    });




                }else {
                    this.$message({
                        showClose: true,
                        message: '请补全信息！',
                        type: 'error'
                    });
                }
            },

            handleChange(value) {
                console.log(value);
            },


            //更新头像
            upimg(){
                this.dialogVisible=true
            },
            //保存
            upsave(){
                if(this.imgurl!=''){
                    let zh = sessionStorage.getItem("zh")
                    const loading = this.$loading({
                        lock: true,
                        text: '正在上传...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                    Api.upTx(this.imgurl,zh)
                    setTimeout(() => {
                        loading.close();
                        window.location.href='./mem.html'
                    }, 2000);
                }else {
                    this.$message({
                        showClose: true,
                        message: '头像为空！',
                        type: 'error'
                    });
                }
            },
            upload(event,file){
                console.log(file)
            },
            handleAvatarSuccess (res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                let flag = res.code
                console.log(res)
                if(flag==0){
                    this.imgurl=res.data
                }else if(flag==2){
                    this.imgurl=res.message
                }
            },
            errorAvatar(err,file){
                console.log("失败")
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isGIF = file.type === 'image/gif';
                const isPNG = file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 1;
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }

                if (!isJPG && !isGIF && !isPNG) {
                    this.$message.error('上传头像图片只能是 JPG、GIF、PNG 格式!');
                }else {
                    if (!isJPG) {
                    }else {
                        return isJPG && isLt2M;
                    }
                    if (!isGIF) {
                    }else {
                        return isGIF && isLt2M;
                    }
                    if (!isPNG) {
                    }else {
                        return isPNG && isLt2M;
                    }
                }
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },

            toMessage(info){
                sessionStorage.setItem("ToMessage",JSON.stringify(info))
                window.location.href="My_Messages.html"
            },


        },
        watch:{
            //根据城市的选择，将数组转换成字符
            city(newv,oldv){
                this.scity=newv.toString()
            }
        }
})


